package dem1;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-07-25
 * Time: 9:39
 */
public class Add extends Work{
    @Override
    public void work(DataSource dataSource) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入名字");
        String name = scanner.next();
        System.out.println("请输入ID");
        int id = scanner.nextInt();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test2?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("123456");

        Connection connection = (Connection) dataSource.getConnection();

        String str = "insert into student values(?, ?)";
        PreparedStatement statement = connection.prepareStatement(str);
        statement.setInt(1,id);
        statement.setString(2,name);

        statement.close();
        connection.close();
    }
}
